package com.sjz.bookmark.tree;

import javax.swing.tree.DefaultMutableTreeNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TreeDataGenerator {

    private static final Random random = new Random();
    private static final String[] NAMES = {"NodeA", "NodeB", "NodeC", "NodeD", "NodeE", "NodeF", "NodeG", "NodeH"};

    public static DefaultMutableTreeNode generateTree() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new MyObject("/", "Root"));
        generateChildren(root, 1, 4); // 从第 1 层开始，最大深度为 4
        return root;
    }

    private static void generateChildren(DefaultMutableTreeNode parent, int currentDepth, int maxDepth) {
        if (currentDepth >= maxDepth) {
            return; // 达到最大深度，停止生成子节点
        }

        int childCount = random.nextInt(10) + 1; // 每个节点随机生成 1-3 个子节点
        for (int i = 0; i < childCount; i++) {
            String name = NAMES[random.nextInt(NAMES.length)] + "-" + currentDepth + "-" + i;
            String path = parent.getUserObject().toString() + "/" + name;
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(new MyObject(path, name));
            parent.add(child);

            // 递归生成子节点
            generateChildren(child, currentDepth + 1, maxDepth);
        }
    }

    public static void main(String[] args) {
        DefaultMutableTreeNode root = generateTree();
        printTree(root, 0);
    }

    private static void printTree(DefaultMutableTreeNode node, int level) {
        for (int i = 0; i < level; i++) {
            System.out.print("  "); // 缩进表示层级
        }
        System.out.println(node.getUserObject());

        // 递归打印子节点
        for (int i = 0; i < node.getChildCount(); i++) {
            printTree((DefaultMutableTreeNode) node.getChildAt(i), level + 1);
        }
    }
}